package com.leyou.search.listrners;

import com.leyou.search.service.IndexService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class GoodsListener {

    private final IndexService indexService;

    public GoodsListener(IndexService indexService) {
        this.indexService = indexService;

    }

    //定义监听器，监听队列
    @RabbitListener(bindings = {
            @QueueBinding(value = @Queue(value = "item.up.queue", durable = "true"),
                    exchange = @Exchange(value = "jhj", durable = "true"),
                    key = "item.up")
    })
    public void handleItemUp(Long spuId) {

        log.info("上架业务，商品id为：{}", spuId);

        //商品索引添加
        this.indexService.addIndex(spuId);
    }

    @RabbitListener(bindings = {
            @QueueBinding(value = @Queue(value = "item.down.queue"),
                    exchange = @Exchange(value = "jhj"),
                    key = "item.down")
    })
    public void handleItemDown(Long spuId) {
        log.info("下架业务，商品id为：{}", spuId);

        //商品索引删除
        this.indexService.deleteIndex(spuId);
    }
}
